package com.imdroid.carac.services;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.IBinder;
import android.os.PowerManager;
import android.util.Log;

public class SoundDetectService extends Service {
	/**
	 * The tag of detect need
	 */
	private static boolean running = true;
	/**
	 * The tag of the sound detection
	 */
	private static String TAG = "SoundDetectService";
	/**
	 * The receiver of the screen broad coast
	 */
	private ScreenBroadcastReceiver screenBroadcastReceiver = new ScreenBroadcastReceiver();
	/**
	 * The wake lock
	 */
	private PowerManager.WakeLock mWakelock;
	/**
	 * The thread of the detect the sound
	 */
	private DetectThread thread;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Service#onBind(android.content.Intent)
	 */
	public IBinder onBind(Intent intent) {
		Log.d(TAG, "SoundDetectService on Bind...");
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Service#onStartCommand(android.content.Intent, int, int)
	 */
	public int onStartCommand(Intent intent, int flags, int startId) {

		if (screenBroadcastReceiver != null) {
			// ע����Ļ�仯������
			IntentFilter intentFilterOn = new IntentFilter(
					"android.intent.action.SCREEN_ON");
			intentFilterOn.setPriority(Integer.MAX_VALUE);
			this.registerReceiver(screenBroadcastReceiver, intentFilterOn);

			IntentFilter intentFilterOff = new IntentFilter(
					"android.intent.action.SCREEN_OFF");
			intentFilterOff.setPriority(Integer.MAX_VALUE);

			this.registerReceiver(screenBroadcastReceiver, intentFilterOn);
			this.registerReceiver(screenBroadcastReceiver, intentFilterOff);
		}
		return START_STICKY;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Service#onDestroy()
	 */
	public void onDestroy() {
		Log.d(this.getClass().getSimpleName(), "on destory...");
		this.unregisterReceiver(screenBroadcastReceiver);
		super.onDestroy();

	}

	/**
	 * The thread to detect the should
	 * 
	 */

	private class DetectThread extends Thread {
		/**
		 * The context
		 */
		private Context context;

		/**
		 * The simple date format
		 */
		// @SuppressLint("SimpleDateFormat")
		// private SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");

		/**
		 * The default constructor
		 * 
		 * @param context
		 *            the context
		 */
		public DetectThread(Context context) {
			this.context = context;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.lang.Thread#run()
		 */
		@SuppressLint("Wakelock")
		public void run() {

			while (running) {
				try {
					AudioManager audioManager = (AudioManager) context
							.getSystemService(Context.AUDIO_SERVICE);
					boolean speakOn = audioManager.isMusicActive();
					// Log.d(TAG, sdf.format(new Date()) + " speakOn - " +
					// speakOn);

					if (speakOn) {
						Thread.sleep(3 * 1000);

						PowerManager pm = (PowerManager) SoundDetectService.this
								.getSystemService(Context.POWER_SERVICE);
						mWakelock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
								| PowerManager.ACQUIRE_CAUSES_WAKEUP
								| PowerManager.ON_AFTER_RELEASE, "SimpleTimer");
						mWakelock.acquire();
						Thread.sleep(3 * 1000);
						if (mWakelock.isHeld())
							mWakelock.release();
					}

					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private class ScreenBroadcastReceiver extends BroadcastReceiver {
		/**
		 * The tag of the broadcast receiver
		 */
		private static final String TAG = "BootBroadcastReceiver";

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * android.content.BroadcastReceiver#onReceive(android.content.Context,
		 * android.content.Intent)
		 */
		public void onReceive(final Context context, Intent intent) {
			Log.d(TAG, intent.getAction());
			// ��Ļ����ʱ��ֹͣ������������
			if (intent.getAction().equals("android.intent.action.SCREEN_ON")) {
				running = false;
				// thread = null;
				// thread.interrupt();
			}
			if (intent.getAction().equals("android.intent.action.SCREEN_OFF")) {
				new Thread() {
					public void run() {
						// ��ĻϨ��ʱ������������������
						try {
							Thread.sleep(10 * 1000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						running = true;
						thread = new DetectThread(context);
						thread.start();

					}
				}.start();
			}
		}
	}
}
